package com.kye.express.business.waybill_take_photo.yunmai;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import com.kye.express.R;

/**
 * 视频取景器控件
 *
 * @author fangcm 2012-09-06
 *
 */
public class ViewfinderView extends View{

	private int width , height;
	private Paint paint;
	private Context mContext;
	private Handler mHandler;
	private int mWidth,mHeight;
	private float lineLeft,lineRight,lineTop,lineBottom;
	private int lineModel = 0;
	private float marginW = 0f;
	private float marginH = 0f;
	private float marginT = 0f;
	private int dLineWidth = 12;
	private int dLen = 60;
	private int m_nImageWidth;
	private int m_nImageHeight;

	boolean l = false, r = false, t = false, b = false , L = false;

	public ViewfinderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}


	public ViewfinderView(Context context){
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}

	public ViewfinderView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}

	/**
	 * 身份证大小  85.6*54
	 * 长是宽的1.58倍
	 * @param pWidth
	 * @param pHeight
	 */
	public void initFinder(int pWidth,int pHeight,Handler mHandler){
		this.mHandler = mHandler;
		m_nImageWidth = pWidth;
		m_nImageHeight = pHeight;
		WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		width = display.getWidth();
		height = display.getHeight();
		marginT = mContext.getResources().getDimension(R.dimen.public_36_dp);

		marginW = (float) ((width - pWidth)/2.0);
		marginH = (float) ((height - pHeight)/2.0);

		mWidth = width/2;
		mHeight = height/2;

		float g = height - marginT * 2;
		float k = g* 1.58f;
		float x = 10.0f;
		Log.d("ocr", k+"<<--k---高度----g--1---->>"+g);
		while(k > pWidth){
			x --;
			k = k * (x/10.0f);
			g = g * (x/10.0f);
		}
		Log.d("ocr", k+"<<--k---高度----g--2---->>"+g);
		lineLeft = (float) (mWidth - k/2.0);
		lineRight = (float) (mWidth + k/2.0);
		lineTop = (float) (mHeight - g/2.0);
		lineBottom = (float) (mHeight + g/2.0);




		int nDisplayWidth = display.getWidth();
		int nDisplayHeight = display.getHeight();

		int nImageWidth = m_nImageWidth;
		int nImageHeight  = m_nImageHeight;
		double nFitWidth;
		double nFitHeight;
		double nUseWidth = 0;
		double nUseHeight = 0;
		double dRealRegionWidth = 0;
//		double dRealRegionHeight = 0;
		if(nImageWidth*nDisplayHeight < nDisplayWidth*nImageHeight){
			nFitHeight = nDisplayHeight;
			nFitWidth = (nImageWidth/(double)nImageHeight)*nFitHeight;
		}else{
			nFitWidth = nDisplayWidth;
			nFitHeight = nFitWidth*(nImageHeight/(double)nImageWidth);
		}
		if(nFitWidth/nFitHeight >= 4/3){
			nUseHeight = nFitHeight;
			nUseWidth = 4*nUseHeight/3.0f;
		}else{
			nUseWidth = nFitWidth;
			nUseHeight = 3*nUseWidth/4.0f;
		}
		dRealRegionWidth = nUseWidth/480.0f*420.0f;
//		dRealRegionHeight = nUseHeight/360.0f*270.0f;

//		lineLeft = (int)((nDisplayWidth - dRealRegionWidth)/2.0f);//- (nDisplayWidth - nFitWidth)/2.0f);
//		lineRight = (int)( nDisplayWidth - lineLeft);//- (nDisplayWidth - nFitWidth) );
//		lineTop = (int)(nDisplayHeight - dRealRegionHeight)/2.0f;
//		lineBottom = nDisplayHeight - lineTop;


		paint = new Paint();
		dLineWidth = (int)dRealRegionWidth/96; //30
		paint.setStrokeWidth(dLineWidth);
		dLen = (int)dRealRegionWidth/10; //160

	}


	public void initFinder(int w,int h,int d){}

	public Rect getFinder(){
		return new Rect((int)(lineLeft - marginW), (int)(lineTop - marginH), (int)(lineRight + marginW), (int)(lineBottom + marginH));
	}


	public void setLineRect(int model){
		lineModel = model;
		invalidate();
	}

	public void setDistance(int distan) {
		distance = -(distan/2);
	}
	private int distance = -80;//边框整体偏向左边的距离
	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.draw(canvas);

		paint.setColor(Color.GREEN);

		canvas.drawLine(lineLeft - dLineWidth/2 + distance, lineTop, lineLeft + dLen + distance, lineTop, paint);
		canvas.drawLine(lineLeft+ distance, lineTop- dLineWidth/2, lineLeft+ distance, lineTop + dLen, paint);

		canvas.drawLine(lineRight + distance, lineTop -  dLineWidth/2, lineRight + distance, lineTop + dLen, paint);
		canvas.drawLine(lineRight +  dLineWidth/2 + distance, lineTop, lineRight - dLen + distance, lineTop, paint);

		canvas.drawLine(lineLeft + distance, lineBottom +  dLineWidth/2, lineLeft + distance, lineBottom - dLen, paint);
		canvas.drawLine(lineLeft -  dLineWidth/2 + distance, lineBottom, lineLeft + dLen + distance, lineBottom, paint);

		canvas.drawLine(lineRight +  dLineWidth/2 + distance, lineBottom, lineRight - dLen + distance, lineBottom, paint);
		canvas.drawLine(lineRight + distance, lineBottom +  dLineWidth/2, lineRight + distance, lineBottom - dLen, paint);

		switch (lineModel) {
			case 0:

				break;
			case 1://左边框线
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				break;
			case 2://右边框线
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				break;
			case 3://左右
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				break;
			case 4://上边框
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				break;
			case 5://左上
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				break;
			case 6://右上
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				break;
			case 7://左右上
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				break;
			case 8://下边框
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				break;
			case 9://左下
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				break;
			case 10://右下边框
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				break;
			case 11://左右下
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				break;
			case 12://上下
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				break;
			case 13://上下左
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				break;
			case 14://上下右
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				break;
			case 15://全
				canvas.drawLine(lineLeft + distance, lineTop, lineLeft + distance, lineBottom, paint);
				canvas.drawLine(lineRight + distance, lineTop, lineRight + distance, lineBottom, paint);
				canvas.drawLine(lineLeft + distance, lineTop, lineRight + distance, lineTop, paint);
				canvas.drawLine(lineLeft + distance, lineBottom, lineRight + distance, lineBottom, paint);
				break;

			default:

				break;
		}
		//框里面的阴影模块
//		paint.setColor(Color.BLACK);
//		paint.setAlpha(100);
//		canvas.drawRect(lineLeft + dLineWidth / 2 + distance, lineTop + dLineWidth / 2, lineRight - dLineWidth / 2 + distance, lineBottom - dLineWidth / 2, paint);
	}

/***********************************点击屏幕自动拍照功能********************************************/
//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		// TODO Auto-generated method stub
//		if(mHandler != null){
//			Log.d("cc_smart", "----CAMERA_TAKE_PHOTO_3--send2->>>");
//			mHandler.sendEmptyMessage(UtilApp.CAMERA_TAKE_PHOTO_3);
//		}
//		return true;
//	}



}
